﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QMSDAL;

namespace QuaManSys.CusSatiSur
{
    public partial class CusSatiSurQuery : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label4.Text = "0";
        }
        string[] customsClass = new string[]{"船厂物资部","船厂设计部", "船厂质量部&船东建造组"
        , "船厂生产部门", "船东机务部门", "所有顾客"};


        protected void btnTotalQuey_Click(object sender, EventArgs e)
        {
            CurUser user = (CurUser)Session["CurUser"];
            if (Permission.JudgePermission(user, "3C", "E0"))
            {
                using (QuaManSysEntities context = new QuaManSysEntities())
                {
                    string no = DDLImpLev.SelectedIndex.ToString();

                    var db = from d in context.CusSatSur
                             select d;
                    if (DDLImpLev.SelectedValue != "null")
                    {
                        db = from d in db
                             where no.Equals(d.infomationId.Substring(3, 1))
                             select d;
                    }

                    if (TextBox1.Text.Trim() != "")
                    {
                        db = from d in db
                             where d.infomationId.Equals(TextBox1.Text.Trim())
                             select d;
                    }

                    if (tbxCtmName.Text.Trim() != "")
                    {
                        db = from d in db
                             where d.customName.Contains(tbxCtmName.Text.Trim())
                             select d;
                    }

                    if (tbxDept.Text.Trim() != "")
                    {
                        db = from d in db
                             where d.department.Contains(tbxDept.Text.Trim())
                             select d;
                    }

                    Label4.Text = db.Count().ToString();
                    foreach (var item in db)
                    {
                        item.customerClass = customsClass[Convert.ToInt16(item.customerClass)];
                    }
                    GridView1.DataSource = db.ToList();
                    GridView1.DataBind();

                }
            }
        }

        protected void btnDeptQuery_Click(object sender, EventArgs e)
        {
            CurUser user = (CurUser)Session["CurUser"];
            if (Permission.JudgePermission(user, "3C", "E1"))
            {
                using (QuaManSysEntities context = new QuaManSysEntities())
                {
                    string no = DDLImpLev.SelectedIndex.ToString();

                    var db = from d in context.CusSatSur
                             select d;
                    if (DDLImpLev.SelectedValue != "null")
                    {
                        db = from d in context.CusSatSur
                             where no.Equals(d.infomationId.Substring(3, 1))
                             select d;
                    }

                    if (TextBox1.Text.Trim() != "")
                    {
                        db = from d in context.CusSatSur
                             where d.infomationId.Equals(TextBox1.Text.Trim())
                             select d;
                    }

                    if (tbxCtmName.Text.Trim() != "")
                    {
                        db = from d in context.CusSatSur
                             where d.customName.Contains(tbxCtmName.Text.Trim())
                             select d;
                    }

                    if (tbxDept.Text.Trim() != "")
                    {
                        db = from d in context.CusSatSur
                             where d.department.Contains(tbxDept.Text.Trim())
                             select d;
                    }

                    
                    foreach (var item in db)
                    {
                        item.customerClass = customsClass[Convert.ToInt16(item.customerClass)];
                    }
                    //CurUser user = (CurUser)Session["CurUser"];
                    List<CusSatSur> dbSource = new List<CusSatSur>();
                    foreach (var item in db)
                    {
                        List<string> dept = Common.CusSatSurRelatedDeptOperation.QueryCurSatSurRelatedDept(item.infomationId);
                        if (dept.Contains(user.department)) dbSource.Add(item);
                    }
                    Label4.Text = dbSource.Count.ToString();
                    GridView1.DataSource = dbSource;
                    GridView1.DataBind();

                }
            }
        }
    }
}